using System;
using System.Collections.Generic;
using System.Text;

namespace Ent.Voice.Nomination
{
    public class ImageProcessor
    {
        public static string GetThumbnailImage(string serverPath, string OriginalFilePath, string ThumbnailFilePath, int NewWidth, int MaxHeight)
        {
            if (System.IO.File.Exists(serverPath + "/" + ThumbnailFilePath))
                return ThumbnailFilePath;
            else if (System.IO.File.Exists(serverPath + "/" + OriginalFilePath))
            {
                ResizeImage(serverPath + "/" + OriginalFilePath, serverPath + "/" + ThumbnailFilePath, NewWidth, MaxHeight, true);
                return ThumbnailFilePath;
            }
            else
                return null;
        }
        public static void ResizeImage(string OriginalFile, string NewFile, int NewWidth, int MaxHeight, bool OnlyResizeIfWider)
        {
            System.Drawing.Image FullsizeImage = System.Drawing.Image.FromFile(OriginalFile);

            // Prevent using images internal thumbnail
            FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
            FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);

            if (OnlyResizeIfWider)
            {
                if (FullsizeImage.Width <= NewWidth)
                {
                    NewWidth = FullsizeImage.Width;
                }
            }

            int NewHeight = FullsizeImage.Height * NewWidth / FullsizeImage.Width;
            if (NewHeight > MaxHeight)
            {
                // Resize with height instead
                NewWidth = FullsizeImage.Width * MaxHeight / FullsizeImage.Height;
                NewHeight = MaxHeight;
            }

            System.Drawing.Image NewImage = FullsizeImage.GetThumbnailImage(NewWidth, NewHeight, null, IntPtr.Zero);
    
          	// Clear handle to original file so that we can overwrite it if necessary
    	    FullsizeImage.Dispose();
    
    	    // Save resized picture
          	NewImage.Save(NewFile);

        }

    }
}
